home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu745.dms / pu745.adf / GLOBE099.LHA / Ami-Globe / 3d.c < prev    next >
C/C++ Source or Header  |  1994-08-24  |  3KB  |  140 lines

  1. #include <exec/types.h>
  2. #include <math.h>
  3. #include "3d_types.h"
  4. #include "3d_protos.h"
  5.  
  6. /****************************************/
  7. /* Fonctions de 3d                                  */
  8. /****************************************/
  9.  
  10. PREFS_3D        Prefs={ 0.0,0.0,0.0,
  11.                                 0,  0,  0,
  12.                                 320,100,
  13.                                 640,200,
  14.                                 300
  15.                             };
  16.  
  17.  
  18. int     dex=300;        /* distance de l'ecran a l'observateur */
  19. int     dey=300;        /* distance de l'ecran a l'observateur */
  20. int     d=6000; /* distance de l'observateur au pt 0   */
  21.  
  22. int     valeur_z=-50;
  23.  
  24. /********************* Rotation dans le plan ************/
  25. void    T3D_2D_Rot(
  26.         float   angle,
  27.         int     *p_x,
  28.         int     *p_y)
  29. {
  30.         int     x,y;
  31.         x= (float)(*p_x) * cos(angle) + (float)(*p_y) * sin(angle);
  32.         y=-(float)(*p_x) * sin(angle) + (float)(*p_y) * cos(angle);
  33.  
  34.         *p_x=(int)x;
  35.         *p_y=(int)y;
  36. }
  37.  
  38. /***************** Calcul de la projection 3D->2D ******/
  39.  
  40. void    calc_coord(
  41.                 int     x,
  42.                 int     y,
  43.                 int     z,
  44.                 int     *p_x,
  45.                 int     *p_y
  46.                 )
  47. {
  48.         if (d+z!=NULL)
  49.     {
  50.                 *p_x=Prefs.centre_x+(x*dex)/(d+z);
  51.                 *p_y=Prefs.centre_y+(y*dey)/(d+z);
  52.         }
  53. }
  54.  
  55.  
  56.  
  57. /***************** Calcul de la projection inverse 2D->3D ******/
  58.  
  59. void    calc_coord_inv(
  60.                 int     x,
  61.                 int     y,
  62.                 int     z,
  63.                 int     *p_x,
  64.                 int     *p_y
  65.                 )
  66. {
  67.         *p_x=( (x - Prefs.centre_x)*(d+z))/dex;
  68.         *p_y=( (y - Prefs.centre_y)*(d+z))/dey;
  69. }
  70.  
  71.  
  72. /* Converti un point du plan de la carte en sa projection 2d */
  73. /* mais en perspective. Pour avoir la coord z, utiliser      */
  74. /* T3D_ConvertZ                                              */
  75.  
  76. void T3D_Convert(int x,int y,int *px,int *py)
  77. {
  78.         int     z;
  79.  
  80.         z=valeur_z*10;
  81.  
  82.         x=10*(x-Prefs.centre_x);
  83.         y=10*(y-Prefs.centre_y);
  84.  
  85.         T3D_2D_Rot(Prefs.az,&x,&y);
  86.         T3D_2D_Rot(Prefs.ax,&y,&z);
  87.         T3D_2D_Rot(Prefs.ay,&z,&x);
  88.  
  89.         calc_coord(x,y,z,px,py);
  90. }
  91.  
  92. /**********************************************************************/
  93.  
  94. void T3D_ConvertZ(int x,int y,int z,int *px,int *py)
  95. {
  96.  
  97.         z=10*(z+valeur_z);
  98.         x=10*(x-Prefs.centre_x);
  99.         y=10*(y-Prefs.centre_y);
  100.  
  101.         T3D_2D_Rot(Prefs.az,&x,&y);
  102.         T3D_2D_Rot(Prefs.ax,&y,&z);
  103.         T3D_2D_Rot(Prefs.ay,&z,&x);
  104.  
  105.         calc_coord(x,y,z,px,py);
  106. }
  107.  
  108.  
  109.  
  110. /**********************************************************************/
  111.  
  112. void T3D_Convert_Inv(int x,int y,int *px,int *py)
  113. {
  114.         int     z;
  115.  
  116.         z=valeur_z;
  117.         calc_coord_inv(x,y,valeur_z,px,py);
  118.         T3D_2D_Rot((float)(2*PI-Prefs.ay),&z,px);
  119.         T3D_2D_Rot((float)(2*PI-Prefs.ax),py,&z);
  120.         T3D_2D_Rot((float)(2*PI-Prefs.az),px,py);
  121.  
  122.         *px=*px+Prefs.centre_x;
  123.         *py=*py+Prefs.centre_y;
  124.  
  125. }
  126.  
  127. /**********************************************************************/
  128.  
  129. PREFS_3D        T3D_Set_Prefs(PREFS_3D In_Prefs)
  130. {
  131.         PREFS_3D Prefs_Temp;
  132.         
  133.         Prefs_Temp =    Prefs;
  134.         Prefs      = In_Prefs;
  135.         dex=Prefs.de * Prefs.sizex/320;
  136.         dey=Prefs.de * Prefs.sizey/200;
  137.  
  138.         return(Prefs_Temp);
  139. }
  140.